﻿using BCCommon;
using BCData.Common.Dictionary;
using BCData.OA.Notification.NotificationCategory;
using BCData.OA.Notification.NotificationCategoryMappingDepartment;
using BCData.Sim.Company.Company;
using BCData.Sim.Company.Department;
using BCDto.OA.Notification.NotificationCategoryMappingDepartment;
using BCEntity.OA.Notification.NotificationCategoryMappingDepartment;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BCService.OA.Notification.NotificationCategoryMappingDepartment
{
    public class NotificationCategoryMappingDepartmentService : INotificationCategoryMappingDepartmentService
    {
        private readonly INotificationCategoryMappingDepartmentData notificationCategoryMappingDepartmentData;
        private readonly ICompanyData companyData;
        private readonly IDepartmentData departmentData;
        private readonly INotificationCategoryData notificationCategoryData;
        private readonly IDictionaryReferenceData dictionaryReferenceData;
        public NotificationCategoryMappingDepartmentService(INotificationCategoryMappingDepartmentData notificationCategoryMappingDepartmentData,
            ICompanyData companyData,
            IDepartmentData departmentData,
            INotificationCategoryData notificationCategoryData,
            IDictionaryReferenceData dictionaryReferenceData)
        {
            this.notificationCategoryMappingDepartmentData = notificationCategoryMappingDepartmentData;
            this.companyData = companyData;
            this.departmentData = departmentData;
            this.notificationCategoryData = notificationCategoryData;
            this.dictionaryReferenceData = dictionaryReferenceData;
        }

        public bool Exists(long notificationCategoryMappingDepartmentId)
        {
            return this.notificationCategoryMappingDepartmentData.Exists(notificationCategoryMappingDepartmentId).GetAwaiter().GetResult();
        }

        public NotificationCategoryMappingDepartmentDto Add(NotificationCategoryMappingDepartmentRequestDto requestDto)
        {

            var entity = CheckRequestData(requestDto); ;

            return this.notificationCategoryMappingDepartmentData.Add(entity).GetAwaiter().GetResult().As<NotificationCategoryMappingDepartmentDto>();
        }
        public NotificationCategoryMappingDepartmentDto Update(NotificationCategoryMappingDepartmentPutDto putDto)
        {
            if (!this.Exists(putDto.NotificationCategoryMappingDepartmentId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var entity = CheckRequestData(putDto);
            entity.NotificationCategoryMappingDepartmentId = putDto.NotificationCategoryMappingDepartmentId;
            return this.notificationCategoryMappingDepartmentData.Update(entity).GetAwaiter().GetResult().As<NotificationCategoryMappingDepartmentDto>();

        }
        private NotificationCategoryMappingDepartmentEntity CheckRequestData(NotificationCategoryMappingDepartmentRequestDto requestDto)
        {
            if (requestDto.NotificationCategoryId == 0)
            {
                throw new ArgumentException("通知分类为必选项");
            }
            if (requestDto.CompanyId == 0)
            {
                throw new ArgumentException("公司为必选项");
            }
            if (requestDto.DepartmentId == 0)
            {
                throw new ArgumentException("部门为必选项");
            }
            if (!requestDto.IsAllJobTitle)
            {
                if (string.IsNullOrWhiteSpace(requestDto.AllowJobTitle))
                {
                    throw new ArgumentException("非允许所有职位显示时，允许显示职位应最少选择一个");
                }
            }
            var notificationCategoryEntity = this.notificationCategoryData.GetEntity(requestDto.NotificationCategoryId).GetAwaiter().GetResult();
            if (notificationCategoryEntity == null)
            {
                throw new ArgumentException("通知分类不存在");
            }
            var companyEntity = this.companyData.GetEntity(requestDto.CompanyId).GetAwaiter().GetResult();
            if (companyEntity == null)
            {
                throw new ArgumentException("此公司不存在");
            }
            var departmentEntities = this.departmentData.GetAll(requestDto.CompanyId).GetAwaiter().GetResult();
            if (!departmentEntities.Any(p => p.DepartmentId == requestDto.DepartmentId))
            {
                throw new ArgumentException(string.Format("{0} 下不存在此部门", companyEntity.CompanyName));
            }
            var departmentEntity = departmentEntities.Single(p => p.DepartmentId == requestDto.DepartmentId);
            //if (string.IsNullOrWhiteSpace(departmentEntity.DepartmentFunction))
            //{
            //    throw new ArgumentException("此部门未设置部门职能，请先设置部门职能");
            //}
            //if (!requestDto.IsAllJobTitle)
            //{
            //    var staffTitleList = dictionaryReferenceData.GetByTypeNameAndParentNameAsync(departmentEntity.DepartmentFunction.Replace("DepartmentFunction_", ""), "StaffTitle").GetAwaiter().GetResult();
            //    string[] titleList = requestDto.AllowJobTitle.Split(',');
            //    foreach (var item in titleList)
            //    {
            //        if (!staffTitleList.Any(p => p.SearchKey == item))
            //        {
            //            throw new ArgumentException(string.Format("此部门不存在员工职位 {0}", item));
            //        }
            //    }
            //}
            var entity = requestDto.As<NotificationCategoryMappingDepartmentEntity>();

            entity.NotificationCategoryId = notificationCategoryEntity.NotificationCategoryId;
            entity.NotificationCategoryCode = notificationCategoryEntity.NotificationCategoryCode;
            entity.NotificationCategoryName = notificationCategoryEntity.NotificationCategoryName;
            entity.CompanyName = companyEntity.CompanyName;
            entity.DepartmentName = departmentEntity.DepartmentName;

            return entity;
        }

        public bool Delete(long notificationCategoryMappingDepartmentId)
        {
            if (!this.Exists(notificationCategoryMappingDepartmentId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.notificationCategoryMappingDepartmentData.Delete(notificationCategoryMappingDepartmentId).GetAwaiter().GetResult();
        }

        public NotificationCategoryMappingDepartmentDto GetEntity(long notificationCategoryMappingDepartmentId)
        {
            return this.notificationCategoryMappingDepartmentData.GetEntity(notificationCategoryMappingDepartmentId).GetAwaiter().GetResult().As<NotificationCategoryMappingDepartmentDto>();
        }

        public IEnumerable<NotificationCategoryMappingDepartmentDto> GetList()
        {
            return this.notificationCategoryMappingDepartmentData.GetList().GetAwaiter().GetResult().As<IEnumerable<NotificationCategoryMappingDepartmentDto>>();
        }

        public PageableList<NotificationCategoryMappingDepartmentDto> Query(string notificationCategoryName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.notificationCategoryMappingDepartmentData.Query(notificationCategoryName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<NotificationCategoryMappingDepartmentDto>
            {
                Items = list.As<IEnumerable<NotificationCategoryMappingDepartmentDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
